<?php
class Bbling_Model_Entity_Business extends Evebit_Model_Abstract {
	
	/**
	 * Name of the resource model
	 *
	 * @var string
	 */
	protected $_resourceName = "Bbling_Model_Resource_Business";
	
	protected $_resourceCollectionName = "Bbling_Model_Collection_Business";
	/* (non-PHPdoc)
	 * @see Evebit_Model_Abstract::_afterLoad()
	 */
	protected function _afterLoad() {
		$data = $this->getData ();
		/*print_r($data);
		exit();*/
		if (! count ( $data ) && !is_array($data)) {
			return;
		}
		$extraData = $this->extraLoad ();
		
		$datas = array_merge ( $extraData ['company'], $extraData ['financial'], $extraData ['marketing'], $extraData ['product'] );
		$this->addData ( $datas );
		$this->addData ( $data );
		
		return $this;
	}
	
	public function extraLoad() {
		
		$data = array();
		if($this->company_id != 0)
		{
			$companyEntity = new Bbling_Model_Entity_Company ();
			$companyEntity->addData ( array ('id' => $this->company_id ) );
			$companyEntity->extraLoad ();
			$data ['company'] = $companyEntity->getData ();
		}
		else
		{
			$data ['company'] = array();
		}
		
		if($this->financial_id != 0)
		{
			$financialEntity = new Bbling_Model_Entity_Financial ();
			$financialEntity->addData ( array ('id' => $this->financial_id ) );
			$financialEntity->extraLoad ();
			$data ['financial'] = $financialEntity->getData ();
		}
		else
		{
			$data ['financial'] = array();
		}
		
		if($this->marketing_id != 0)
		{		
		  $marketingEntity = new Bbling_Model_Entity_Marketing ();
		  $marketingEntity->addData ( array ('id' => $this->marketing_id ) );
		  $marketingEntity->extraLoad ();
		  $data ['marketing'] = $marketingEntity->getData ();
		}
		else
		{
			$data ['marketing'] = array();
		}
		
		if($this->product_id != 0)
		{
			$productEntity = new Bbling_Model_Entity_Product ();
			$productEntity->addData ( array ('id' => $this->product_id ) );
			$productEntity->extraLoad ();
			$data ['product'] = $productEntity->getData ();						
		}
		else
		{
			$data ['product'] = array();
		}
		return $data;
	}
	/**
	 * (non-PHPdoc)
	 * @see Evebit_Model_Abstract::_beforeSave()
	 */
	protected function _beforeSave() {
		$user = Zend_Auth::getInstance ()->getIdentity ();
		$data ['user_id'] = $user->id;
		
		$datas = $this->getData ();
		$data ['id'] = $datas ['id'] ? $datas ['id'] : null;
		$data ['published'] = isset ( $datas ['published'] ) ? $datas ['published'] : 0;
		
		/*$adapter = new Zend_File_Transfer_Adapter_Http ();
		$a = $adapter->getFileInfo();
		if ($a['data_company_cv_files_']['name']){
			$adapter->setDestination (RP . "/media/cvUpload/");
		    $adapter->receive();
		    $datas['company_cv_files'] = $a['data_company_cv_files_']['name'];
		}*/
		$companyEntity = new Bbling_Model_Entity_Company ();
		$companyEntity->addData ( $datas );
		$companyEntity->extraSave ();
		$data ['company_id'] = $companyEntity->id;
		
		$financialEntity = new Bbling_Model_Entity_Financial ();
		$financialEntity->addData ( $datas );
		$financialEntity->extraSave ();
		$data ['financial_id'] = $financialEntity->id;
		
		$marketingEntity = new Bbling_Model_Entity_Marketing ();
		$marketingEntity->addData ( $datas );
		$marketingEntity->extraSave ();
		$data ['marketing_id'] = $marketingEntity->id;
		
		$productEntity = new Bbling_Model_Entity_Product ();
		$productEntity->addData ( $datas );
		$productEntity->extraSave ();
		$data ['product_id'] = $productEntity->id;
		
		$this->unsetData ();
		$this->addData ( $data );
		return $this;
	}
	
	/**
	 * (non-PHPdoc)
	 * @return Evebit_Model_Abstract::_beforeDelete()
	 */
	protected function _beforeDelete() {
		$data = $this->getData();
		
		$companyEntity = new Bbling_Model_Entity_Company ();
		$companyEntity->addData(array('id'=>$data['company_id']));
		$companyEntity->extraDelete();
		
		$financialEntity = new Bbling_Model_Entity_Financial ();
		$financialEntity->addData(array('id'=>$data['financial_id']));
		$financialEntity->extraDelete();
		
		$marketingEntity = new Bbling_Model_Entity_Marketing ();
		$marketingEntity->addData(array('id'=>$data['marketing_id']));
		$marketingEntity->extraDelete();
		
		$productEntity = new Bbling_Model_Entity_Product ();
		$productEntity->addData(array('id'=>$data['product_id']));
		$productEntity->extraDelete();
		
		return $this;
	}
	
	public function getTableId($id, $tableName) {
		$this->load ( $id );
		$field = strtolower ( $tableName ) . '_id';
		return $this->$field;
	}
	
	public function getSpecialEntity($name) {
		$className = 'Bbling_Model_Entity_' . ucfirst ( $name );
		try {
			class_exists ( $className );
			$entity = new $className ();
			return $entity;
		} catch ( Exception $e ) {
			$this->getSession ()->addError ( $this->__ ( 'The ' . $className . ' not exists' ) );
			exit ();
		}
	}
	
	public function getSpecialType($name, $isList = false, $IsAddTop = false, $text = 'Select a Type', $col = 'type') {
		$entity = $this->getSpecialEntity ( $name );
		$types = $entity->getCollection ()->getColumnValues ( $col );
		
		if ($isList) {
			$keys = $types;
			$values = null;
			foreach ( $types as $type ) {
				if (strpos ( $type, '_' )) {
					$type = str_replace ( '_', ' ', $type );
				}
				$values [] = ucwords ( $type );
			}
			if ($IsAddTop) {
				array_unshift ( $keys, '*' );
				array_unshift ( $values, $text );
			}
			$types = array_combine ( $keys, $values );
		}
		return $types;
	}
	
	public function getFundList() {
		$fundEntity = new Bbling_Model_Entity_Fund ();
		return $fundEntity->getList ();
	}
	
	public function saveBalanaceDatas($saleDatas) {
		$datas = $this->getData ();
		$balanaceEntity = new Bbling_Model_Entity_Balanace ();
		$financialId = $this->getTableId ( $this->id, 'financial' );
		$datas ['financial_id'] = $financialId;
		$balanaceEntity->addData ( $datas );
		$balanaceEntity->saveBalanaceDatas ( $saleDatas );
	}
}
	
